package com.atfcm.link;

/**
 * @Author whaty
 * @create 2024/11/1 13:46
 */
public class t328奇偶链表 {


    /**
     * 我的解法
     * @param head
     * @return
     */
    public ListNode oddEvenList(ListNode head) {
        if (head == null) return head;
        ListNode nummy = new ListNode(-1);
        nummy.next = head;
        ListNode nummyCur = nummy;
        ListNode nummyO = new ListNode(-1);
        ListNode nummy0cur = nummyO;

        while (nummyCur.next != null && nummyCur.next.next != null) {
            nummy0cur.next = nummyCur.next.next;
            nummyCur.next.next = nummyCur.next.next.next == null ? null : nummyCur.next.next.next;
            nummyCur = nummyCur.next;
            nummy0cur = nummy0cur.next;
        }
        nummy0cur.next = null;
        if (nummyCur.next != null) {
            nummyCur.next.next = nummyO.next;
        } else {
            nummyCur.next = nummyO.next;
        }
        return nummy.next;

    }

    /**
     * 官方解法
     * @param head
     * @return
     */
    public ListNode oddEvenList2(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode odd = head;
        ListNode even = head.next;
        ListNode newEveHead = new ListNode(-1);
        newEveHead.next = even;

        while (even != null && even.next != null) {
            System.out.println(1);
            odd.next = even.next;
            even.next = even.next.next;
            odd = odd.next;
            even = even.next;
        }

        odd.next = newEveHead.next;

        return head;

    }


    static class ListNode {
        public int val;
        public ListNode next;

        ListNode(int val) {
            this.val = val;
        }
    }
}
